package com.bytedance.common.plugin.interfaces.pushmanager.message;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;

import com.bytedance.common.plugin.interfaces.pushmanager.MessageConstants;
import com.bytedance.common.plugin.interfaces.pushmanager.PushPluginDepend;
import com.bytedance.common.plugin.interfaces.pushmanager.client.SsPushManager;
import com.bytedance.common.plugin.interfaces.pushmanager.setting.PushSetting;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class PushRegisterResultHandler implements Callback {
    private static final String KEY_ERR_CODE = "errcode";
    private static final String KEY_ERR_MSG = "errmsg";
    private static final String KEY_SENDER = "sender";
    private static final String KEY_TOKEN = "token";
    private static final int MSG_SEND_PUSH_DELAY_TIME = 15000;
    private static final int MSG_SEND_PUSH_REGISTER_RESLUT_DELAY = 1;
    private static final int MSG_SEND_PUSH_REGISTER_RESULT = 0;
    private static final String TAG = "PushRegisterResultHandl";
    private WeakHandler mHandler;
    private String mLastSendsString;
    private Map mPushRegisterResultMap;
    private static volatile PushRegisterResultHandler sInstance;

    private PushRegisterResultHandler() {
        this.mPushRegisterResultMap = new LinkedHashMap();
        this.mHandler = new WeakHandler(Looper.getMainLooper(), ((Callback) this));
    }

    public static String addUrlParam(String arg5, Map arg6) {
        ArrayList v1 = new ArrayList();
        if (arg6 != null) {
            Iterator v2 = arg6.entrySet().iterator();
            while (v2.hasNext()) {
                Object v0 = v2.next();
                ((List) v1).add(new Pair(((Map.Entry) v0).getKey(), ((Map.Entry) v0).getValue()));
            }
        }

        return PushRegisterResultHandler.addUrlParam(arg5, ((List) v1));
    }

    public static String addUrlParam(String arg2, List arg3) {
        StringBuilder v0 = new StringBuilder(arg2);
        if (arg2.indexOf(63) < 0) {
            v0.append("?");
        } else {
            v0.append("&");
        }

        v0.append(android.support.design.a.a(arg3, "UTF-8"));
        return v0.toString();
    }

    public void handleMsg(Message message) {
        final JSONArray senders;
        if (message != null) {
            switch (message.what) {
                case 0:
                case 1: {
                    Class v1 = PushRegisterResultHandler.class;
                    synchronized (v1) {
                        senders = this.mapToArray(this.mPushRegisterResultMap);
                        if (senders == null) {
                            return;
                        }
                    }

                    if (message.what == 1 && this.mLastSendsString != null && (this.mLastSendsString.equals(senders.toString()))) {
                        return;
                    }

                    this.mHandler.removeMessages(0);
                    com.bytedance.common.utility.b.a.a(new AsyncTask() {
                        @Override
                        protected Object doInBackground(Object[] arg8) {
                            boolean v0_4;
                            Object v6 = null;
                            boolean v1 = false;
                            if (senders != null) {
                                try {
                                    if (Logger.debug()) {
                                        Logger.d("PushRegisterResultHandl", "doInBackground: 发送注册结果 senders = " + senders);
                                    }

                                    JSONObject v0_1 = new JSONObject();
                                    v0_1.put("did", PushSetting.getInstance().getDeviceId());
                                    v0_1.put("senders", senders);
                                    v0_1.put("push_sdk", new JSONArray(PushSetting.getInstance().getPushChannelsJsonArray()));
                                    String v2 = PushRegisterResultHandler.addUrlParam(MessageConstants.PUSH_REGISTER_RESULT_URL, SsPushManager.inst().getHttpCommonParams());
                                    ArrayList v3 = new ArrayList();
                                    ((List) v3).add(new Pair("register_data", v0_1.toString()));
                                    String v0_2 = PushPluginDepend.inst().post(v2, ((List) v3));
                                    if (Logger.debug()) {
                                        Logger.d("PushRegisterResultHandl", "doInBackground: response = " + v0_2);
                                    }
                                    if (!android.support.design.a.isTextEmpty(v0_2)) {
                                        if ("success".equals(new JSONObject(v0_2).getString("reason"))) {
                                            v0_4 = true;
                                            PushRegisterResultHandler.this.mLastSendsString = senders.toString();
                                            v1 = v0_4;
                                        } else {
                                            v0_4 = false;
                                            v1 = v0_4;
                                        }
                                    }
                                    return v6;
                                } catch (JSONException v0_3) {
                                    v0_3.printStackTrace();
                                    return v6;
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    return v6;
                                } finally {
                                    if (!Logger.debug()) {
                                        return v6;
                                    }
                                    Logger.d("PushRegisterResultHandler", "postPushRegisterResult send result = " + v1);
                                }
                            }
                            return v6;
                        }
                    }, new Object[0]);
                    return;
                }
                default: {
                    return;
                }
            }
        }
    }

    public static PushRegisterResultHandler inst() {
        if (PushRegisterResultHandler.sInstance == null) {
            Class v1 = PushRegisterResultHandler.class;
            synchronized (v1) {
                PushRegisterResultHandler.sInstance = new PushRegisterResultHandler();
            }
        }

        return PushRegisterResultHandler.sInstance;
    }

    private JSONArray mapToArray(Map arg4) {
        JSONArray v0_1;
        JSONArray v1 = new JSONArray();
        if (arg4 != null && !arg4.isEmpty()) {
            try {
                Iterator<Entry> v2 = arg4.entrySet().iterator();
                while (v2.hasNext()) {
                    v1.put(v2.next().getValue());
                }
            } catch (Throwable v0) {
                v0.printStackTrace();
            }

            v0_1 = v1;
        } else {
            v0_1 = v1;
        }

        return v0_1;
    }

    public void onAppStart(Context arg8) {
        Class v5_1;
        int v4;
        JSONObject v3;
        int v0_2;
        if (arg8 != null) {
            try {
                String v0_1 = PushSetting.getInstance().getPushRegisterResults();
                if (android.support.design.a.isTextEmpty(v0_1)) {
                    this.mHandler.sendEmptyMessageDelayed(1, 120000);
                    return;
                }

                JSONArray v1 = new JSONArray(v0_1);
                int v2 = v1.length();
                v0_2 = 0;
                while (true) {
                    if (v0_2 >= v2) {
                        this.mHandler.sendEmptyMessageDelayed(1, 120000);
                        return;
                    }

                    v3 = v1.optJSONObject(v0_2);
                    int v5 = -1;
                    v4 = v3.optInt("sender", v5);
                    if (v4 > 0) {
                        v5_1 = PushRegisterResultHandler.class;
                        synchronized (v5_1) {
                            this.mPushRegisterResultMap.put(Integer.valueOf(v4), v3);
                        }
                        ++v0_2;
                        continue;
                    }
                    ++v0_2;
                    continue;
                }
            } catch (Throwable v0) {
                this.mHandler.sendEmptyMessageDelayed(1, 120000);
            }
        }
    }

    public void onPushRegisterFail(Context arg3, final int push_type, final String msg) {
        if (arg3 != null && !android.support.design.a.isTextEmpty(msg)) {
            com.bytedance.common.utility.b.a.a(new AsyncTask() {
                @Override
                protected Object doInBackground(Object[] arg5) {
                    JSONArray v0_2;
                    Class v1;
                    JSONObject v0_1;
                    try {
                        v0_1 = new JSONObject();
                        v0_1.put("sender", push_type);
                        v0_1.put("errcode", 1);
                        v0_1.put("errmsg", msg);
                        if (Logger.debug()) {
                            Logger.d("PushRegisterResultHandler", "onPushRegisterFail json = " + v0_1);
                        }
                        v1 = PushRegisterResultHandler.class;
                        synchronized (v1) {
                            PushRegisterResultHandler.this.mPushRegisterResultMap.put(Integer.valueOf(push_type), v0_1);
                            v0_2 = PushRegisterResultHandler.this.mapToArray(PushRegisterResultHandler.this.mPushRegisterResultMap);
                        }
                        if (v0_2 == null) {
                        } else {
                            PushSetting.getInstance().setPushRegisterResults(v0_2.toString());
                        }
                        if (PushRegisterResultHandler.this.mHandler.hasMessages(0)) {
                            return null;
                        }
                        PushRegisterResultHandler.this.mHandler.sendEmptyMessageDelayed(0, 15000);

                    } catch (Throwable v0) {

                    }

                    return null;
                }
            }, new Object[0]);
        }
    }

    public void onPushRegisterSuccess(Context arg3, final int push_type, final String token) {
        if (arg3 != null && !android.support.design.a.isTextEmpty(token)) {
            com.bytedance.common.utility.b.a.a(new AsyncTask() {
                protected Object doInBackground(Object[] arg6) {
                    JSONArray v0_2;
                    Class v1;
                    JSONObject v0_1;
                    try {
                        v0_1 = new JSONObject();
                        v0_1.put("sender", push_type);
                        v0_1.put("errcode", 0);
                        v0_1.put("token", token);
                        if (Logger.debug()) {
                            Logger.d("PushRegisterResultHandler", "onPushRegisterSuccess json = " + v0_1);
                        }

                        v1 = PushRegisterResultHandler.class;
                        synchronized (v1) {
                            PushRegisterResultHandler.this.mPushRegisterResultMap.put(Integer.valueOf(push_type), v0_1);
                            v0_2 = PushRegisterResultHandler.this.mapToArray(PushRegisterResultHandler.this.mPushRegisterResultMap);
                        }
                        if (v0_2 == null) {
                        } else {
                            PushSetting.getInstance().setPushRegisterResults(v0_2.toString());
                        }


                        label_37:
                        if (!PushRegisterResultHandler.this.mHandler.hasMessages(0)) {
                            PushRegisterResultHandler.this.mHandler.sendEmptyMessageDelayed(0, 15000);
                        }
                        return null;
                    } catch (Exception e) {

                    }
                    return null;
                }
            }, new Object[0]);
        }
    }
}

